clear
set more off
capture log close

/*-------------------------------------------
Cost-Effectiveness of Strategies to Improve Eyeglass Access among School Children: 
Evidence from a Randomized Experiment

Last Edit: 21 April 2020 by SS & XCM
-------------------------------------------*/
clear 
use Ordeal_2020.dta, clear

global xlist male grade5 paredu_high pamigrant asset_index1 distance ///
logmar2 baseown aware0 paharmvision0 eyeexercise0 		

global sch_xlist sch_male sch_grade5 sch_paredu_high sch_pamigrant sch_asset_index1 sch_distance /// 
sch_logmar2 sch_baseown sch_aware0 sch_paharmvision0 sch_eyeexercise0 


/*-------------------------------------------
Table 1: Experimental Design
-------------------------------------------*/

tab edu major if schone==1
tab edu major 


/*-------------------------------------------
Table 2: Descriptive Statistics and Balance Check 
-------------------------------------------*/
est clear
foreach var of varlist $xlist {
eststo: quietly areg `var' free voune voue conne cone, cluster(schid) absorb(strata)
test free voune voue conne cone
estadd scalar p_diff = r(p)

su `var' if frene==1
estadd scalar mean = r(mean)
estadd scalar sd= r(sd)


}
esttab using balance.csv, replace label b(3) se(3)  scalars(p_diff mean sd) ///
star(* 0.10 ** 0.05 *** 0.01)  wide keep(free voune voue conne cone) nogap 


/*-------------------------------------------
Table 3: Eyeglass Acquisition and Ownership
Y = edu vou vou_edu con con_edu + Xb + e
free as left out comparison
Table
Columns (1) and (2) -School Level
Columns (3) and (4) -Individual Level
-------------------------------------------*/

*School Level
eststo clear
	eststo: areg sch_own1 edu vou vou_edu con con_edu if schone==1, robust absorb(strata)
		su sch_own1 if schone==1 & frene==1
		estadd scalar mean=r(mean)
	eststo: areg sch_own2 edu vou vou_edu con con_edu if schone==1, robust absorb(strata)
		su sch_own2 if schone==1 & frene==1
		estadd scalar mean=r(mean)
	
esttab using uptake.csv, replace star(* 0.10 ** 0.05 *** 0.01) ///
b(3) se(3) ar2 keep (edu vou vou_edu con con_edu) nogap scalars(mean)

*Individual Level
eststo clear
	eststo: areg own1 edu vou vou_edu con con_edu, vce(cluster schid) absorb(strata)
			su own1 if frene==1
			estadd scalar mean=r(mean)
	eststo: areg own2 edu vou vou_edu con con_edu, vce(cluster schid) absorb(strata)
			su own2 if frene==1
			estadd scalar mean=r(mean)
	
esttab using uptake.csv, append star(* 0.10 ** 0.05 *** 0.01) ///
b(3) se(3) ar2 keep (edu vou vou_edu con con_edu) nogap scalars(mean)


/*-------------------------------------------
Table 4: Correlates of Eyeglass Ownership at Long-Term Follow-Up
Table
Column (1): Subsample of Voucher, No Information
Column (2): Subsample of Voucher, Information
Column (3): Subsample of Market, No Information
Column (4): Subsample of Market, Information
-------------------------------------------*/
*Informed Index
*recode to 1=informed
aindex paharm_ind aware0 faglass eyeex_ind nowearpupil_ind, gen(inform_ind)

su inform_ind, det
	g informed = (inform_ind>`r(p50)')
		replace informed=. if inform_ind==.

global uptake_corr logmar2 dist_quint2-dist_quint5 asset_quint2-asset_quint5 baseown goodlook0 informed male grade5 paredu_high pamigrant i.countyid 

eststo clear
	eststo: xi: reg own2 $uptake_corr if vou==1 & edu==0, vce(cluster schid)  
	eststo: xi: reg own2 $uptake_corr if vou==1 & edu==1, vce(cluster schid)  
	eststo: xi: reg own2 $uptake_corr if con==1 & edu==0, vce(cluster schid)  
	eststo: xi: reg own2 $uptake_corr if con==1 & edu==1, vce(cluster schid) 
	

esttab using Uptake_corr.csv, replace star(* 0.10 ** 0.05 *** 0.01) ///
b(3) se(3) ar2 nogap drop(_I*)


/*-------------------------------------------
Table 5: Eyeglass Usage
Y = edu vou vou_edu con con_edu + Xb + e
free as left out comparison
Table
Panel A: Conditional Usage
Panel B: Unconditional Usage("Effective Coverage")
Columns (1) and (2): Usage during unannounced check
Columns (3) and (4): Self-reported: Use eyeglasses
Columns (5) and (6): Self-reported: Regularly wears eyeglasses
-------------------------------------------*/

*Table

*Panel A: Conditional Usage
eststo clear
	eststo: areg sch_conduse_wear1 edu vou vou_edu con con_edu if schone==1, robust absorb(strata)
		su sch_conduse_wear1 if schone==1 & frene==1
		estadd scalar mean=r(mean)
		
	eststo: areg wear2 edu vou vou_edu con con_edu if own2==1, vce(cluster schid) absorb(strata)
			su wear2 if e(sample)==1 &  frene==1
		estadd scalar mean=r(mean)
	
	eststo: areg use1 edu vou vou_edu con con_edu if own1==1, vce(cluster schid) absorb(strata)
		su use1 if  e(sample) &  frene==1
		estadd scalar mean=r(mean)
		
	eststo: areg use2 edu vou vou_edu con con_edu if own2==1, vce(cluster schid) absorb(strata)
		su use2 if  e(sample) &  frene==1
		estadd scalar mean=r(mean)
	
	eststo: areg aluse1 edu vou vou_edu con con_edu if own1==1, vce(cluster schid) absorb(strata)
		su aluse1 if  e(sample) &  frene==1
		estadd scalar mean=r(mean)
	
	eststo: areg aluse2 edu vou vou_edu con con_edu if own2==1, vce(cluster schid) absorb(strata)
		su aluse2 if  e(sample) &  frene==1
		estadd scalar mean=r(mean)
	
esttab using usage.csv, replace star(* 0.10 ** 0.05 *** 0.01) ///
b(3) se(3) ar2 keep (edu vou vou_edu con con_edu) nogap scalar(mean)

*Panel B: Unconditional Usage("Effective Coverage")
eststo clear
	eststo: areg sch_wear1 edu vou vou_edu con con_edu if schone==1, robust absorb(strata)
		su sch_wear1 if  e(sample) &  frene==1
		estadd scalar mean=r(mean)
	
	eststo: areg wear2 edu vou vou_edu con con_edu, vce(cluster schid) absorb(strata)
		su wear2 if  e(sample) &  frene==1
		estadd scalar mean=r(mean)
	
	eststo: areg use1 edu vou vou_edu con con_edu, vce(cluster schid) absorb(strata)
		su use1 if  e(sample) &  frene==1
		estadd scalar mean=r(mean)
	
	eststo: areg use2 edu vou vou_edu con con_edu, vce(cluster schid) absorb(strata)
		su use2 if  e(sample) &  frene==1
		estadd scalar mean=r(mean)
	
	eststo: areg aluse1 edu vou vou_edu con con_edu, vce(cluster schid) absorb(strata)
		su aluse1 if  e(sample) &  frene==1
		estadd scalar mean=r(mean)
	
	eststo: areg aluse2 edu vou vou_edu con con_edu, vce(cluster schid) absorb(strata)
		su aluse2 if  e(sample) &  frene==1
		estadd scalar mean=r(mean)
	
esttab using usage.csv, append star(* 0.10 ** 0.05 *** 0.01) ///
b(3) se(3) ar2 keep (edu vou vou_edu con con_edu) nogap scalar(mean)


/*-------------------------------------------
Figure 1: Acquisition and Usage by Experimental Group
-------------------------------------------*/

		***Short Term
		**Acquisition and Usage
		*sch_own1
		areg sch_own1 free voune voue conne cone, vce(cluster schid) absorb(strata)

		mat B = e(b)

		g Serr = . in 1/6
		local i=1
		foreach var in _cons free voune voue conne cone {
			replace Serr = _se[`var'] in `i'
			local i = `i'+1
			}


		mat B=B'
		svmat B

		g B2 = B1[_n-1]

		su sch_own1 if frene==1 & schone==1
			replace B2 = B2+`r(mean)'
			replace B2 =1 in 1
			replace B2 =1 in 2
			replace B2 = . in 7

		drop B1
		
		*sch_wear1
		areg sch_wear1 free voune voue conne cone if schone==1, robust absorb(strata)

		mat B = e(b)

		local i=7
		foreach var in _cons free voune voue conne cone {
			replace Serr = _se[`var'] in `i'
			local i = `i'+1
			}
			

		mat B=B'
		svmat B

		g B3 = B1[_n-7]
		replace B3 = 0 in 7

		su sch_wear1 if frene==1 & schone==1
			replace B3 = B3+`r(mean)'
			replace B3 = . in 13
		replace B2 = B3 if B2==.
		drop B1 B3
		
		g n = 1 in 1
		replace n = 1 in 7

		replace n = 3 in 3
		replace n = 3 in 9

		replace n = 5 in 5
		replace n = 5 in 11


		replace n = 8 in 2
		replace n = 8 in 8

		replace n = 10 in 4
		replace n = 10 in 10

		replace n = 12 in 6
		replace n = 12 in 12
		
		serrbar B2 Serr n, scale(1.96) addplot(bar B2 n, below )   ///
		ylabel(0(0.2)1) xlabel(none) legend(off) xtitle("A. Short-term") ytitle("Proportion")

        drop  B2 n Serr
		
	
		***Long Term
		**Acquisition and Usage
		*own2
		areg own2 free voune voue conne cone , vce(cluster schid) absorb(strata)

		mat B = e(b)
		mat B=B'
		svmat B

		g B2 = B1[_n-1]

		g Serr = . in 1/6
		local i=1
		foreach var in _cons free voune voue conne cone {
			replace Serr = _se[`var'] in `i'
			local i = `i'+1
			}

		su own2 if frene==1
			replace B2 = B2+`r(mean)'
			replace B2 =1 in 1
			replace B2 =1 in 2
			replace B2 = . in 7


		drop B1
		*wear2
		areg wear2 free voune voue conne cone, vce(cluster schid) absorb(strata)

		mat B = e(b)
		mat B=B'
		svmat B

		g B3 = B1[_n-7]
		replace B3 = 0 in 7

		local i=7
		foreach var in _cons free voune voue conne cone {
			replace Serr = _se[`var'] in `i'
			local i = `i'+1
			}

		su wear2 if frene==1 
			replace B3 = B3+`r(mean)'
			replace B3 = . in 13
		replace B2 = B3 if B2==.
		drop B1 B3

		g n = 1 in 1
		replace n = 1 in 7

		replace n = 3 in 3
		replace n = 3 in 9

		replace n = 5 in 5
		replace n = 5 in 11


		replace n = 8 in 2
		replace n = 8 in 8

		replace n = 10 in 4
		replace n = 10 in 10

		replace n = 12 in 6
		replace n = 12 in 12

		serrbar B2 Serr n, scale(1.96) addplot(bar B2 n, below )   ///
		ylabel(0(0.2)1) xlabel(none) legend(off) xtitle("B. Long-term") ytitle("Proportion")

        drop  B2 n Serr		
		
ANALYSIS COMPLETE
 
*********************************************************************************


